package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import ch.rheintalconnection.sysventory.technicalServices.IHostScanner;
import ch.rheintalconnection.sysventory.technicalServices.IIpScanner;
 
public class IpScanner implements IIpScanner {
    private static final int TIMEOUT = 15000;
    
    private ArrayList<String> adressen;
 
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> enumInterfaces = NetworkInterface.getNetworkInterfaces();
            while (enumInterfaces.hasMoreElements()) {
                NetworkInterface currentInterface = enumInterfaces.nextElement();
                if (currentInterface.isLoopback() == false && currentInterface.isUp()) {
                    Enumeration<InetAddress> enumAdresses = currentInterface.getInetAddresses();
                    while (enumAdresses.hasMoreElements()) {
                        InetAddress currentAddress = enumAdresses.nextElement();
                        new IpScanner(currentAddress);
                    }
                }
            }
          
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
 
    private IpScanner(InetAddress ia) {
        String[] sAddress = ia.getHostAddress().split("[.]");
        String lanAddress = sAddress[0] + "." + sAddress[1] + "." + sAddress[2] + ".";
        
        adressen = new ArrayList<String>();
        
        for (short s = 1; s < 255; s++) {
            new AddressScanner(lanAddress + s);
            //Aufruf WMI Abfrage sli
        }
    }
 
    public ArrayList<String> getAdressen() {
		return adressen;
	}

	public void setAdressen(ArrayList<String> adressen) {
		this.adressen = adressen;
	}

	private class AddressScanner implements Runnable {
        private String sInetAddress = null;
 
        private AddressScanner(String inetAddress) {
            this.sInetAddress = inetAddress;
            new Thread(this).start();
        }
 
        public void run() {
            try {
                InetAddress ia = InetAddress.getByName(this.sInetAddress);
                if(ia.isReachable(TIMEOUT) || !ia.getCanonicalHostName().equalsIgnoreCase(this.sInetAddress) || !ia.getHostName().equalsIgnoreCase(this.sInetAddress)) {
                    adressen.add(this.sInetAddress);
                	//System.out.println("Reached " + this.sInetAddress + "(" + ia.getCanonicalHostName() + ")");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

	public List<IHostScanner> scanIps() {
		// TODO Auto-generated method stub
		return null;
	}
}